package com.roundarchisobar.njariwala.netflix.service.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.roundarchisobar.njariwala.netflix.constants.NetflixConstants;
import com.roundarchisobar.njariwala.netflix.service.NetflixUtilityService;

@Component
public class NetflixUtilityServiceImpl implements NetflixUtilityService , NetflixConstants{

	private static final Logger logger = LoggerFactory.getLogger(NetflixUtilityServiceImpl.class);

	

	private static final char[] HEX_CHARS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
			'e', 'f' };

	public String getEncodedString(String input) {
		String str = null;
		try {
			str = URLEncoder.encode(input, UTF_8);
		} catch (UnsupportedEncodingException e) {
			logger.error("getEncodedString failed");
		}
		return str;
	}

	public String generateRandomHexString(int len) {
		StringBuilder hexString = new StringBuilder(len);
		for (int i = 0; i < len; i++) {
			int j = (int) (Math.random() * 16);
			hexString.append(HEX_CHARS[j]);
		}
		return hexString.toString();
	}
	
	public String getNonce(){
		return generateRandomHexString(16);
	}
	
	public String getTimestamp(){
		return System.currentTimeMillis() / 1000L + "";
	}
}
